package com.example.applicationview.anim;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.applicationview.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Property Animator - ValueAnimator ObjectAnimator
 * 属性动画的使用
 * 属性动画添加插值器
 * 属性动画自定义Evaluator
 */
public class ActivityPropertyAnimation extends AppCompatActivity {

    @BindView(R.id.view)
    View myView;
    @BindView(R.id.text_view)
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anim);
        ButterKnife.bind(this);
    }

    public void alpha(View view) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 1200);
        valueAnimator.setDuration(2000);
        valueAnimator.start();
        //通过监听属性值，修改view的属性 达到动画效果
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int values = (int) animation.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = myView.getLayoutParams();
                layoutParams.width = values;

                myView.setLayoutParams(layoutParams);
            }
        });
    }

    public void scale(View view) {
        ValueAnimator animator = ValueAnimator.ofInt(0, 600);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int curValue = (int) animation.getAnimatedValue();
                myView.layout(myView.getLeft(), curValue, myView.getRight(), curValue + myView.getHeight());
            }
        });
        animator.setDuration(1000);
        //属性动画，添加 插值器
        animator.setInterpolator(new BounceInterpolator());
        animator.start();
    }

    public void translate(View view) {
        //自定义字符Evaluator
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new CharObject(), new Character('A'), new Character('Z'));
        valueAnimator.setDuration(20000);
        valueAnimator.start();
        //通过监听属性值，修改view的属性 达到动画效果
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                char values = (char) animation.getAnimatedValue();
                textView.setText(String.valueOf(values));
            }
        });

    }

    public void rotate(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(myView,"alpha",1,0,1);
        animator.setDuration(2000);
        animator.start();

    }

    public void set(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(myView,"rotation",0,360);
        animator.setDuration(2000);
        animator.start();

    }
}
